package com.yenlien.traceyun.breed.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.breed.entity.BreedDetail;
import com.yenlien.traceyun.webase.dao.BaseJpaDao;

public interface BreedDetailDao extends BaseJpaDao<BreedDetail> {

    List<BreedDetail> findByRelevanceId(String relevanceId);

    List<BreedDetail> findByRelevanceIdAndSource(String relevanceId, String source);

    List<BreedDetail> findByRelevanceIdAndSourceAndIsOut(String relevanceId, String source, int isout);

    BreedDetail findByEquipmentId(String equipmentId);

    @Query(value = "select equipment_id equipmentId,equipment_name equipmentName from t_breed_detail where relevance_id = ?1 and (equipment_id != '' or equipment_id is not null)", nativeQuery = true)
    List<JSONObject> findEquipment(String breedId);

    @Query(value = "select equipment_id from t_breed_detail where relevance_id = ?1", nativeQuery = true)
    List<String> findEquipmentIds(String breedId);

    BreedDetail findByCode(String code);

    List<BreedDetail> findByBatch(String batch);


    List<BreedDetail> findByTypeAndCode(String type, String code);

    @Query(value = "select * from t_breed_detail where type = ?1 and code = ?2 and source = ?3", nativeQuery = true)
    BreedDetail findBreedDetail(String type, String code, String source);

    @Query(value = "select * from t_breed_detail where type = ?1 and code in (?2) and source = ?3 and is_out = ?4 and batch = ?5", nativeQuery = true)
    BreedDetail findBreedDetail(String type, String code, String source, int isOut, String batch);

    @Query(value = "select * from t_breed_detail where type = ?1 and code in (?2) and is_out = ?3 and batch = ?4", nativeQuery = true)
    BreedDetail findBreedDetail(String type, String code, int isOut, String batch);

    @Query(value = "select * from t_breed_detail where type = ?1 and code in (?2) and source = '养殖'", nativeQuery = true)
    List<BreedDetail> findByTypeAndCodeList(String type, List<String> codeList);

    /**
     * 根据养殖id删除详细
     *
     * @param relevanceId
     */
    public void deleteByRelevanceId(String relevanceId);
}
